package com.vision.service.impl;

import com.vision.mapper.LikeVideoMapper;
import com.vision.pojo.entity.LikeVideo;
import com.vision.service.LikeVideoService;
import com.vision.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @projectName: vision
 * @package: com.vision.service.impl
 * @className: LikeVideoServiceImpl
 * @author: escapeicon@gmail.com
 * @description: 视频点赞服务实现
 * @date: 2024/1/28 10:14
 * @version: 1.0
 */
@Service
public class LikeVideoServiceImpl implements LikeVideoService {

    @Autowired
    private LikeVideoMapper likeVideoMapper;

    /**
     * 添加视频点赞
     * @param videoId 视频id
     */
    @Override
    public void addLikeVideo(Integer videoId) {
        // 1. 获取当前用户 id
        Map<String,Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 查询是否已点赞
        LikeVideo likeVideo = likeVideoMapper.selectByUserIdAndVideoId(userId, videoId);
        if (likeVideo != null) {
            throw new RuntimeException("该用户已点赞");
        }
        // 3. 添加点赞记录
        likeVideoMapper.insert(userId,videoId);
    }

    /**
     * 取消视频点赞
     * @param videoId 视频id
     */
    @Override
    public void cancelLikeVideo(Integer videoId) {
        // 1. 获取当前用户 id
        Map<String,Integer> claims = ThreadLocalUtil.get();
        Integer userId = claims.get("id");
        // 2. 删除点赞记录
        likeVideoMapper.deleteByVideoIdAndUserId(userId,videoId);
    }
}
